1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gtk.DropTarget;
26 
27 private import gdk.ContentFormats;
28 private import gdk.Drop;
29 private import glib.ConstructionException;
30 private import gobject.ObjectG;
31 private import gobject.Signals;
32 private import gobject.Value;
33 private import gtk.EventController;
34 private import gtk.c.functions;
35 public  import gtk.c.types;
36 private import std.algorithm;
37 
38 
39 /**
40  * `GtkDropTarget` is an event controller to receive Drag-and-Drop operations.
41  * 
42  * The most basic way to use a `GtkDropTarget` to receive drops on a
43  * widget is to create it via [ctor@Gtk.DropTarget.new], passing in the
44  * `GType` of the data you want to receive and connect to the
45  * [signal@Gtk.DropTarget::drop] signal to receive the data:
46  * 
47  * ```c
48  * static gboolean
49  * on_drop (GtkDropTarget *target,
50  * const GValue  *value,
51  * double         x,
52  * double         y,
53  * gpointer       data)
54  * {
55  * MyWidget *self = data;
56  * 
57  * // Call the appropriate setter depending on the type of data
58  * // that we received
59  * if (G_VALUE_HOLDS (value, G_TYPE_FILE))
60  * my_widget_set_file (self, g_value_get_object (value));
61  * else if (G_VALUE_HOLDS (value, GDK_TYPE_PIXBUF))
62  * my_widget_set_pixbuf (self, g_value_get_object (value));
63  * else
64  * return FALSE;
65  * 
66  * return TRUE;
67  * }
68  * 
69  * static void
70  * my_widget_init (MyWidget *self)
71  * {
72  * GtkDropTarget *target =
73  * gtk_drop_target_new (G_TYPE_INVALID, GDK_ACTION_COPY);
74  * 
75  * // This widget accepts two types of drop types: GFile objects
76  * // and GdkPixbuf objects
77  * gtk_drop_target_set_gtypes (target, (GTypes [2]) {
78  * G_TYPE_FILE,
79  * GDK_TYPE_PIXBUF,
80  * }, 2);
81  * 
82  * g_signal_connect (target, "drop", G_CALLBACK (on_drop), self);
83  * gtk_widget_add_controller (GTK_WIDGET (self), GTK_EVENT_CONTROLLER (target));
84  * }
85  * ```
86  * 
87  * `GtkDropTarget` supports more options, such as:
88  * 
89  * * rejecting potential drops via the [signal@Gtk.DropTarget::accept] signal
90  * and the [method@Gtk.DropTarget.reject] function to let other drop
91  * targets handle the drop
92  * * tracking an ongoing drag operation before the drop via the
93  * [signal@Gtk.DropTarget::enter], [signal@Gtk.DropTarget::motion] and
94  * [signal@Gtk.DropTarget::leave] signals
95  * * configuring how to receive data by setting the
96  * [property@Gtk.DropTarget:preload] property and listening for its
97  * availability via the [property@Gtk.DropTarget:value] property
98  * 
99  * However, `GtkDropTarget` is ultimately modeled in a synchronous way
100  * and only supports data transferred via `GType`. If you want full control
101  * over an ongoing drop, the [class@Gtk.DropTargetAsync] object gives you
102  * this ability.
103  * 
104  * While a pointer is dragged over the drop target's widget and the drop
105  * has not been rejected, that widget will receive the
106  * %GTK_STATE_FLAG_DROP_ACTIVE state, which can be used to style the widget.
107  * 
108  * If you are not interested in receiving the drop, but just want to update
109  * UI state during a Drag-and-Drop operation (e.g. switching tabs), you can
110  * use [class@Gtk.DropControllerMotion].
111  */
112 public class DropTarget : EventController
113 {
114 	/** the main Gtk struct */
115 	protected GtkDropTarget* gtkDropTarget;
116 
117 	/** Get the main Gtk struct */
118 	public GtkDropTarget* getDropTargetStruct(bool transferOwnership = false)
119 	{
120 		if (transferOwnership)
121 			ownedRef = false;
122 		return gtkDropTarget;
123 	}
124 
125 	/** the main Gtk struct as a void* */
126 	protected override void* getStruct()
127 	{
128 		return cast(void*)gtkDropTarget;
129 	}
130 
131 	/**
132 	 * Sets our main struct and passes it to the parent class.
133 	 */
134 	public this (GtkDropTarget* gtkDropTarget, bool ownedRef = false)
135 	{
136 		this.gtkDropTarget = gtkDropTarget;
137 		super(cast(GtkEventController*)gtkDropTarget, ownedRef);
138 	}
139 
140 
141 	/** */
142 	public static GType getType()
143 	{
144 		return gtk_drop_target_get_type();
145 	}
146 
147 	/**
148 	 * Creates a new `GtkDropTarget` object.
149 	 *
150 	 * If the drop target should support more than 1 type, pass
151 	 * %G_TYPE_INVALID for @type and then call
152 	 * [method@Gtk.DropTarget.set_gtypes].
153 	 *
154 	 * Params:
155 	 *     type = The supported type or %G_TYPE_INVALID
156 	 *     actions = the supported actions
157 	 *
158 	 * Returns: the new `GtkDropTarget`
159 	 *
160 	 * Throws: ConstructionException GTK+ fails to create the object.
161 	 */
162 	public this(GType type, GdkDragAction actions)
163 	{
164 		auto __p = gtk_drop_target_new(type, actions);
165 
166 		if(__p is null)
167 		{
168 			throw new ConstructionException("null returned by new");
169 		}
170 
171 		this(cast(GtkDropTarget*) __p, true);
172 	}
173 
174 	/**
175 	 * Gets the actions that this drop target supports.
176 	 *
177 	 * Returns: the actions that this drop target supports
178 	 */
179 	public GdkDragAction getActions()
180 	{
181 		return gtk_drop_target_get_actions(gtkDropTarget);
182 	}
183 
184 	/**
185 	 * Gets the currently handled drop operation.
186 	 *
187 	 * If no drop operation is going on, %NULL is returned.
188 	 *
189 	 * Returns: The current drop
190 	 *
191 	 * Since: 4.4
192 	 */
193 	public Drop getCurrentDrop()
194 	{
195 		auto __p = gtk_drop_target_get_current_drop(gtkDropTarget);
196 
197 		if(__p is null)
198 		{
199 			return null;
200 		}
201 
202 		return ObjectG.getDObject!(Drop)(cast(GdkDrop*) __p);
203 	}
204 
205 	/**
206 	 * Gets the currently handled drop operation.
207 	 *
208 	 * If no drop operation is going on, %NULL is returned.
209 	 *
210 	 * Deprecated: Use [method@Gtk.DropTarget.get_current_drop] instead
211 	 *
212 	 * Returns: The current drop
213 	 */
214 	public Drop getDrop()
215 	{
216 		auto __p = gtk_drop_target_get_drop(gtkDropTarget);
217 
218 		if(__p is null)
219 		{
220 			return null;
221 		}
222 
223 		return ObjectG.getDObject!(Drop)(cast(GdkDrop*) __p);
224 	}
225 
226 	/**
227 	 * Gets the data formats that this drop target accepts.
228 	 *
229 	 * If the result is %NULL, all formats are expected to be supported.
230 	 *
231 	 * Returns: the supported data formats
232 	 */
233 	public ContentFormats getFormats()
234 	{
235 		auto __p = gtk_drop_target_get_formats(gtkDropTarget);
236 
237 		if(__p is null)
238 		{
239 			return null;
240 		}
241 
242 		return ObjectG.getDObject!(ContentFormats)(cast(GdkContentFormats*) __p);
243 	}
244 
245 	/**
246 	 * Gets the list of supported `GType`s that can be dropped on the target.
247 	 *
248 	 * If no types have been set, `NULL` will be returned.
249 	 *
250 	 * Returns: the `G_TYPE_INVALID`-terminated array of types included in
251 	 *     formats
252 	 */
253 	public GType[] getGtypes()
254 	{
255 		size_t nTypes;
256 
257 		auto __p = gtk_drop_target_get_gtypes(gtkDropTarget, &nTypes);
258 
259 		return __p[0 .. nTypes];
260 	}
261 
262 	/**
263 	 * Gets whether data should be preloaded on hover.
264 	 *
265 	 * Returns: %TRUE if drop data should be preloaded
266 	 */
267 	public bool getPreload()
268 	{
269 		return gtk_drop_target_get_preload(gtkDropTarget) != 0;
270 	}
271 
272 	/**
273 	 * Gets the current drop data, as a `GValue`.
274 	 *
275 	 * Returns: The current drop data
276 	 */
277 	public Value getValue()
278 	{
279 		auto __p = gtk_drop_target_get_value(gtkDropTarget);
280 
281 		if(__p is null)
282 		{
283 			return null;
284 		}
285 
286 		return ObjectG.getDObject!(Value)(cast(GValue*) __p);
287 	}
288 
289 	/**
290 	 * Rejects the ongoing drop operation.
291 	 *
292 	 * If no drop operation is ongoing, i.e when [property@Gtk.DropTarget:current-drop]
293 	 * is %NULL, this function does nothing.
294 	 *
295 	 * This function should be used when delaying the decision
296 	 * on whether to accept a drag or not until after reading
297 	 * the data.
298 	 */
299 	public void reject()
300 	{
301 		gtk_drop_target_reject(gtkDropTarget);
302 	}
303 
304 	/**
305 	 * Sets the actions that this drop target supports.
306 	 *
307 	 * Params:
308 	 *     actions = the supported actions
309 	 */
310 	public void setActions(GdkDragAction actions)
311 	{
312 		gtk_drop_target_set_actions(gtkDropTarget, actions);
313 	}
314 
315 	/**
316 	 * Sets the supported `GTypes` for this drop target.
317 	 *
318 	 * Params:
319 	 *     types = all supported `GType`s
320 	 *         that can be dropped on the target
321 	 */
322 	public void setGtypes(GType[] types)
323 	{
324 		gtk_drop_target_set_gtypes(gtkDropTarget, types.ptr, cast(size_t)types.length);
325 	}
326 
327 	/**
328 	 * Sets whether data should be preloaded on hover.
329 	 *
330 	 * Params:
331 	 *     preload = %TRUE to preload drop data
332 	 */
333 	public void setPreload(bool preload)
334 	{
335 		gtk_drop_target_set_preload(gtkDropTarget, preload);
336 	}
337 
338 	/**
339 	 * Emitted on the drop site when a drop operation is about to begin.
340 	 *
341 	 * If the drop is not accepted, %FALSE will be returned and the drop target
342 	 * will ignore the drop. If %TRUE is returned, the drop is accepted for now
343 	 * but may be rejected later via a call to [method@Gtk.DropTarget.reject]
344 	 * or ultimately by returning %FALSE from a [signal@Gtk.DropTarget::drop]
345 	 * handler.
346 	 *
347 	 * The default handler for this signal decides whether to accept the drop
348 	 * based on the formats provided by the @drop.
349 	 *
350 	 * If the decision whether the drop will be accepted or rejected depends
351 	 * on the data, this function should return %TRUE, the
352 	 * [property@Gtk.DropTarget:preload] property should be set and the value
353 	 * should be inspected via the ::notify:value signal, calling
354 	 * [method@Gtk.DropTarget.reject] if required.
355 	 *
356 	 * Params:
357 	 *     drop = the `GdkDrop`
358 	 *
359 	 * Returns: %TRUE if @drop is accepted
360 	 */
361 	gulong addOnAccept(bool delegate(Drop, DropTarget) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
362 	{
363 		return Signals.connect(this, "accept", dlg, connectFlags ^ ConnectFlags.SWAPPED);
364 	}
365 
366 	/**
367 	 * Emitted on the drop site when the user drops the data onto the widget.
368 	 *
369 	 * The signal handler must determine whether the pointer position is in
370 	 * a drop zone or not. If it is not in a drop zone, it returns %FALSE
371 	 * and no further processing is necessary.
372 	 *
373 	 * Otherwise, the handler returns %TRUE. In this case, this handler will
374 	 * accept the drop. The handler is responsible for using the given @value
375 	 * and performing the drop operation.
376 	 *
377 	 * Params:
378 	 *     value = the `GValue` being dropped
379 	 *     x = the x coordinate of the current pointer position
380 	 *     y = the y coordinate of the current pointer position
381 	 *
382 	 * Returns: whether the drop was accepted at the given pointer position
383 	 */
384 	gulong addOnDrop(bool delegate(Value, double, double, DropTarget) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
385 	{
386 		return Signals.connect(this, "drop", dlg, connectFlags ^ ConnectFlags.SWAPPED);
387 	}
388 
389 	/**
390 	 * Emitted on the drop site when the pointer enters the widget.
391 	 *
392 	 * It can be used to set up custom highlighting.
393 	 *
394 	 * Params:
395 	 *     x = the x coordinate of the current pointer position
396 	 *     y = the y coordinate of the current pointer position
397 	 *
398 	 * Returns: Preferred action for this drag operation or 0 if
399 	 *     dropping is not supported at the current @x,@y location.
400 	 */
401 	gulong addOnEnter(GdkDragAction delegate(double, double, DropTarget) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
402 	{
403 		return Signals.connect(this, "enter", dlg, connectFlags ^ ConnectFlags.SWAPPED);
404 	}
405 
406 	/**
407 	 * Emitted on the drop site when the pointer leaves the widget.
408 	 *
409 	 * Its main purpose it to undo things done in
410 	 * [signal@Gtk.DropTarget::enter].
411 	 */
412 	gulong addOnLeave(void delegate(DropTarget) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
413 	{
414 		return Signals.connect(this, "leave", dlg, connectFlags ^ ConnectFlags.SWAPPED);
415 	}
416 
417 	/**
418 	 * Emitted while the pointer is moving over the drop target.
419 	 *
420 	 * Params:
421 	 *     x = the x coordinate of the current pointer position
422 	 *     y = the y coordinate of the current pointer position
423 	 *
424 	 * Returns: Preferred action for this drag operation or 0 if
425 	 *     dropping is not supported at the current @x,@y location.
426 	 */
427 	gulong addOnMotion(GdkDragAction delegate(double, double, DropTarget) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
428 	{
429 		return Signals.connect(this, "motion", dlg, connectFlags ^ ConnectFlags.SWAPPED);
430 	}
431 }